#include "LineChart.h"
/**
 *  Write the necessary junk for the top of the chart
 */
void writeTopBoilerPlateOfLineChart(std::ostream& out) {
    out << "<html>\n";
    out << "<head>\n";
    out << "<!--Load the AJAX API-->\n";
    out << "<script type=\"text/javascript\"";
    out << "src=\"https://www.google.com/jsapi\">";
    out << "</script>\n";
    out << "<script type=\"text/javascript\">\n";
    out << "google.load('visualization', '1.0',";
    out << " {'packages':['corechart','line']});\n";
    out << "google.setOnLoadCallback(drawChart);\n";
    out << "function drawChart() {\n";
    out << "var data=new google.visualization.DataTable();";
    out << "\n";
    out << "data.addColumn('string', 'x');\n";
    out << "data.addColumn('number', 'y');\n";

}


/**
 *  Write the intesting bit of the Line Chart
 */
void writeDataOfLineChart(std::ostream& out,
    const std::vector<double>& labels,
    const std::vector<double>& values) {
    out << "data.addRows([\n";
    size_t nLabels = labels.size();
    for (size_t i = 0; i < nLabels; i++) {
        double label = labels[i];
        double value = values[i];
        out << "['" << label << "', " << value << "]";
        if (i != nLabels - 1) {
            out << ",";
        }
        out << "\n";
    }
    out << "]);\n";

}

/**
 *  Write the boring bit at the bottom of a pie chart
 */
void writeBottomBoilerPlateOfLineChart(
    std::ostream& out,
    const std::string& xLabelName,
    const std::string& yLabelName,
    int length,
    int width) {
    out << "var options = {'title':'A Line Chart',\n";
    out << "'hAxis':{'title':'" << xLabelName << "'}, \n";
    out << "'vAxis':{'title':'" << yLabelName << "'}, \n";
    /*std::cout << "'hAxis':{'title':" << xLabelName << "}, \n";
    std::cout << "'vAxis':{'title':" << yLabelName << "}, \n";
    std::cout << "'hAxis':{'title':'x'}, \n";
    std::cout << "'vAxis':{'title':'y'}, \n";
    out << "'hAxis':{'title':'x'}, \n";
    out << "'vAxis':{'title':'y'}, \n";*/
    out << "'width':" << length << ", \n";
    out << "'height':" << width << "\n";
    out << "};\n";
    out << "var chart = new google.visualization.LineChart(";
    out << "document.getElementById('chart_div'));\n";
    out << "chart.draw(data, options);\n";
    out << "}\n";
    out << "</script>\n";
    out << "</head>\n";
    out << "<body>\n";
    out << "<div id='chart_div'>\n";
    out << "</body>\n";
    out << "</html>";
}


LineChart::LineChart() {
    m_title = "figure";
    m_labels = {};
    m_values = {};
    m_xLabelName = "x";
    m_yLabelName = "y";
    m_length = 300;
    m_width = 400;
}


void LineChart::setTitle(const std::string& title) {
    m_title = title;
}

void LineChart::setXLabelName(const std::string& xName) {
    m_xLabelName = xName;
}
void LineChart::setYLabelName(const std::string& yName) {
    m_yLabelName = yName;
}
void LineChart::setLength(int length) {
    m_length = length;
}
void LineChart::setWidth(int width) {
    m_width = width;
}

void LineChart::addEntry(double label, double value) {
    m_labels.push_back(label);
    m_values.push_back(value);
}

void LineChart::writeAsHTML(std::ostream& out) const {
    writeTopBoilerPlateOfLineChart(out);
    writeDataOfLineChart(out, m_labels, m_values);
    writeBottomBoilerPlateOfLineChart(out, m_xLabelName, m_yLabelName, m_length, m_width);
}


void LineChart::writeAsHTML(const std::string& file) const {
    std::ofstream out;
    out.open(file.c_str());
    writeAsHTML(out);
    out.close();
}